"""
管理员视图层
"""
from db.models import Admin, Teacher
from lib.utils import prettyprint, loop_input
from interface import common_interface, admin_interface
from lib.utils import login_auth

login_user = {'username': ''}


def register():
    """管理员注册"""
    while True:
        username = input('请输入注册的管理员姓名:').strip()
        password = input('请输入密码:').strip()
        re_password = input('请再次输入密码:').strip()

        if password != re_password:  # 两次密码不一致
            print('两次输入的密码不一致！请重新输入！')
            continue

        admin_obj = Admin(username, password)
        success, msg = common_interface.register_interface(admin_obj, 0)  # 调用公共注册接口
        print(msg)  # 打印消息
        if success:  # 注册成功
            break


def login():
    """管理员登录"""
    username = input('请输入管理员姓名:').strip()
    password = input('请输入密码:').strip()

    success, msg = common_interface.login_interface(username, password, 0)  # 调用公共登录接口
    print(msg)  # 打印消息
    if success:  # 登录成功
        login_user['username'] = username


@login_auth(login, login_user)
def create_school():
    """创建学校"""
    while True:
        school_name = input('请输入创建的学校名:').strip()
        address = input('请输入创建的学校地址:').strip()

        success, msg = admin_interface.create_school_interface(school_name, address)  # 调用创建学校接口
        print(msg)  # 打印消息
        if success:  # 创建成功
            break


@login_auth(login, login_user)
def create_course():
    """创建课程"""
    while True:
        # 调用接口打印学校
        school_name_list = common_interface.list_school_interface()
        if not school_name_list:
            print('当前学校列表为空，请先新建学校！')
            return
        # 选择学校
        index = int(loop_input('请输入课程创建的学校序号:', '输入错误,请重新输入！',
                               lambda x, y: int(x) in range(1, len(y) + 1) if x.isdigit() else False, school_name_list))
        school_name = school_name_list[index - 1]
        # 调用接口打印学校下的课程
        common_interface.list_course_interface(school_name)
        # 输入课程信息
        course_name = input('请输入创建的课程名:').strip()
        price = loop_input('请输入课程价格:', '价格必须为大于0的数字！请重新输入！', lambda x: x.isdigit())
        # 调用创建课程接口
        success, msg = admin_interface.create_course_interface(school_name, course_name, price)
        print(msg)  # 打印消息
        if success:  # 创建成功
            break


@login_auth(login, login_user)
def create_teacher():
    """创建教师"""
    while True:
        teacher_name = input('请输入创建的教师姓名:').strip()
        password = input('请输入密码:').strip()
        age = loop_input('请输入教师年龄:', '年龄必须为0~120的数字！请重新输入！',
                         lambda x: int(x) <= 120 if x.isdigit() else False)
        gender = loop_input('请选择教师性别[1-男][2-女]:', '必须输入数字1[男]或2[女]！请重新输入！',
                            lambda x: x in ['1', '2'])
        teacher_obj = Teacher(teacher_name, password, age, gender)
        success, msg = common_interface.register_interface(teacher_obj, 1)  # 调用公共注册接口
        print(msg)  # 打印消息
        if success:  # 创建成功
            break


admin_dict = {
    '1': register,
    '2': login,
    '3': create_school,
    '4': create_course,
    '5': create_teacher,
    'T': None,
    'Q': None
}


def run():
    while True:
        username = login_user["username"] if login_user["username"] else '未登录'  # 当前登录的用户名
        prettyprint(
            f'当前管理员:{username}',
            ['编号', '功能'],
            [['【1】', '管理员注册'],
             ['【2】', '管理员登录'],
             ['【3】', '创建学校'],
             ['【4】', '创建课程'],
             ['【5】', '创建老师']],
            8)
        order = loop_input('请输入功能编号[Q返回][T登出]:', '输入错误请重新输入！', lambda x, y: x in y, admin_dict)

        if order == 'Q':  # 返回主视图
            login_user['username'] = ''  # 将当前用户设置为空
            return

        if order == 'T':  # 退出登录
            if username == '未登录':
                print('无法退出登录，请先登录！')
            else:
                login_user['username'] = ''  # 将当前用户设置为空
            continue

        admin_dict[order]()
        input('按回车继续...')
